// pages/poster/index.js
const {
  post,
  dateFormat
} = getApp();
Page({
  data: {
    params: {
      page: 1,
      pageSize: 10
    },
    dataList: [],
    showPage: false,
    showLoading: true,
    showCon: false
  },
  // 初始化加载
  onLoad: function () {
    this.getList();
  },
  // 获取数据
  getList() {
    if (this.data.showLoading) {
      post("/spread/poster/list", this.data.params).then(res => {
        if (res.result == 1) {
          const dataList = this.data.dataList.concat(res.data);
          this.setData({
            dataList,
            showPage: true
          });
          if (res.data.length < this.data.params.pageSize) {
            this.setData({
              showLoading: false
            })
          }
        };
      });
    }
  },
  // 上啦加载
  onReachBottom: function () {
    this.setData({
      "params.page": this.data.params.page += 1
    });
    this.getList();
  },
  // 下载
  downloadImg(e) {
    const _this = this;
    wx.downloadFile({
      url: e.currentTarget.dataset.url,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              wx.getSetting({
                withSubscriptions: true,
                success: (res) => {
                  if (!res.authSetting['scope.writePhotosAlbum']) {
                    _this.setData({
                      showCon: true
                    })
                  }
                }
              });
            }
          }
        })
      }
    });
  },
  // 关闭
  changeModalCancel() {
    this.setData({
      showCon: false
    })
  },
  // 预览
  previewImage(e) {
    const arry = this.data.dataList.map(item => item.image);
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: arry
    });
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})